package com.briup.myblog.config;

import com.briup.myblog.web.interceptor.DeleteInterceptor;
import com.briup.myblog.web.interceptor.JWTInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author lining
 * @Date 2022/6/15
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired//从ioc容器获取该类型的对象
    private JWTInterceptor jwtInterceptor;//如何赋值？
    @Autowired
    private DeleteInterceptor deleteInterceptor; //删除拦截器

    @Override//注解拦截器对象
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                // 拦截器会拦截/auth/所有子路径的访问
                .addPathPatterns("/auth/**")
                .excludePathPatterns("/auth/user/info");

        //删除拦截器
        registry.addInterceptor(deleteInterceptor)
                .addPathPatterns("/auth/user/delete") //删除用户
                .addPathPatterns("/auth/config/deleteById")//删除系统配置
                .addPathPatterns("/auth/comment/batchDelete")//批量删除评论
                .addPathPatterns("/auth/comment/deleteById")//根据id删除评论
                .addPathPatterns("/auth/article/changeStatus")//修改资讯信息状态
                .addPathPatterns("/auth/article/deleteById") //id删除资讯信息
                .addPathPatterns("/auth/category/batchDelete")//批量删除资讯栏目
                .addPathPatterns("/auth/category/deleteById") //id删除资讯栏目
                .addPathPatterns("/auth/carousel/**")//轮播图模块除了查找使用外的全部
                .addPathPatterns("/auth/category/update")//更新栏目
                .addPathPatterns("/auth/user/delete")//删除栏目
                .addPathPatterns("/auth/comment/check")//审查评论
                .addPathPatterns("/auth/comment/deleteById")//删除评论
                .addPathPatterns("/auth/comment/batchDelete")//批量删除
                .addPathPatterns("/auth/config/update/**")//更新系统配置
                .addPathPatterns("/auth/config/save")//保存系统配置
                .addPathPatterns("/auth/config/deleteById")//根据ID删除系统配置
                .excludePathPatterns("/auth/carousel/find/yes")//查找轮播图
                .excludePathPatterns("/auth/carousel/page/query");
    }

    /**
     * 设置跨域请求的处理逻辑：
     * addMapping 指可以被允许访问的资源
     * allowCredentials 指是否可以使用cookie
     * allowedHeaders 指请求头的类型
     * allowedOriginPatterns 指可以访问该站点的客户端（用户使用的站点）
     * allowedMethods 指可以使用的访问方式
     * maxAge 指options请求缓存在浏览器中的最大时间，时间到了浏览器会自动清楚，之后会重新发送
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //任何资源
        registry.addMapping("/**")
        //禁用cookie
        .allowCredentials(false)
        .allowedHeaders("*")
        .allowedOriginPatterns("*")
        //"OPTIONS" 预检请求
        .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
        .maxAge(3600);
    }

}
